﻿using Dapper;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using System.Data;
using System.Data.SqlClient;

namespace HR.Services.PengService
{
    public class GongGongShuXingServices : IGongGongShuXingServices
    {
        private readonly IConfiguration conf;
        private readonly string? conStr;

        public GongGongShuXingServices(IConfiguration conf)
        {
            this.conf = conf;
            conStr = this.conf["ConnectionStrings"];
        }
        public Task<int> Add(GongGongShuXing gongGongShuXing)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr)) 
                {
                    return con.Execute($"insert into config_public_char(attribute_kind,attribute_name) values('{gongGongShuXing.Attribute_kind}','{gongGongShuXing.Attribute_name}')");
                }
            });
        }

        public Task<int> Delete(int pbc_id)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"delete from config_public_char where pbc_id='{pbc_id}'");
                }
            });
        }

        public Task<List<GongGongShuXing>> FindAll()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Query<GongGongShuXing>($"select * from config_public_char").ToList();
                }
            });
        }

        public Task<FenYe<GongGongShuXing>> GongGongFenYe(int size, int currentPage)
        {
            return Task.Run(() =>
            {
                using (SqlConnection conn = new SqlConnection(conStr))
                {
                    DynamicParameters dynamicParameters = new DynamicParameters();
                    dynamicParameters.Add("@pageSize", size);
                    dynamicParameters.Add("@keyName", "pbc_id");
                    dynamicParameters.Add("@tabelName", "config_public_char");
                    dynamicParameters.Add("@where", "");
                    dynamicParameters.Add("@currentPage", currentPage);
                    dynamicParameters.Add("@lie", "*");
                    dynamicParameters.Add("@rows", dbType: DbType.Int32, direction: ParameterDirection.Output);
                    string sql = "exec proc_Fenye @pageSize, @keyName, @tabelName, @where, @currentPage, @lie, @rows out";
                    List<GongGongShuXing> list = conn.Query<GongGongShuXing>(sql, dynamicParameters).ToList();
                    int rows = dynamicParameters.Get<int>("@rows");
                    FenYe<GongGongShuXing> fenYe = new FenYe<GongGongShuXing>()
                    {
                        Rows = rows,
                        GongGongShuXings = list
                    };
                    return fenYe;
                }
            });
          }
        

        public Task<int> Update(GongGongShuXing gongGongShuXing)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"update config_public_char set attribute_kind='{gongGongShuXing.Attribute_kind}',attribute_name='{gongGongShuXing.Attribute_name}' where pbc_id='{gongGongShuXing.Pbc_id}'");
                }
            });
        }
    }
}
